var CalendarData = new Array(20);
var madd = new Array(12);

var tgString = "甲乙丙丁戊己庚辛壬癸";
var dzString = "子丑寅卯辰巳午未申酉戌亥";

var numString = "一二三四五六七八九十";
var monthString = "正二三四五六七八九十冬腊";

var zodiacSign = "鼠牛虎兔龙蛇马羊猴鸡狗猪";

var weekString = "日一二三四五六";

var year;
var month;
var day;
var datetime;

function initLuncalendar() {
	CalendarData[0] = 0x41A95;
	CalendarData[1] = 0xD4A;
	CalendarData[2] = 0xDA5;
	CalendarData[3] = 0x20B55;
	CalendarData[4] = 0x56A;
	CalendarData[5] = 0x7155B;
	CalendarData[6] = 0x25D;
	CalendarData[7] = 0x92D;
	CalendarData[8] = 0x5192B;
	CalendarData[9] = 0xA95;
	CalendarData[10] = 0xB4A;
	CalendarData[11] = 0x416AA;
	CalendarData[12] = 0xAD5;
	CalendarData[13] = 0x90AB5;
	CalendarData[14] = 0x4BA;
	CalendarData[15] = 0xA5B;
	CalendarData[16] = 0x60A57;
	CalendarData[17] = 0x52B;
	CalendarData[18] = 0xA93;
	CalendarData[19] = 0x40E95;

	madd[0] = 0;
	madd[1] = 31;
	madd[2] = 59;
	madd[3] = 90;
	madd[4] = 120;
	madd[5] = 151;
	madd[6] = 181;
	madd[7] = 212;
	madd[8] = 243;
	madd[9] = 273;
	madd[10] = 304;
	madd[11] = 334;

	var nowDate = new Date();
	year = nowDate.getYear();
	month = nowDate.getMonth();
	day = nowDate.getDay();
	datetime = nowDate.getDate();

}

function e2c() {
	var totalmnk;
	var isEnd = false;

	if (year < 1900)
		year += 1900;

	total = (year - 2001) * 365 + Math.floor((year - 2001) / 4) + madd[month]
			+ datetime - 23;

	if (year % 4 == 0 && month > 1)
		total++;

	for (m = 0;; m++) {
		k = (CalendarData[m] < 0xfff) ? 11 : 12;

		for (n = k; n >= 0; n--) {
			if (total <= 29 + getBit(CalendarData[m], n)) {
				isEnd = true;
				break;
			}

			total = total - 29 - getBit(CalendarData[m], n);
		}

		if (isEnd)
			break;
	}

	year = 2001 + m;
	month = k - n + 1;
	day = total;

	if (k == 12) {
		if (month == Math.floor(CalendarData[m] / 0x10000) + 1)
			month = 1 - month;
		if (month > Math.floor(CalendarData[m] / 0x10000) + 1)
			month--;
	}
}

function getBit(m, n) {
	return (m >> n) & 1;
}

function getLunarCalendar() {
	initLuncalendar();
	e2c();

	var tmp = "";
	tmp += tgString.charAt((year - 4) % 10); // 年干
	tmp += dzString.charAt((year - 4) % 12); // 年支
	tmp += "年【";
	tmp += zodiacSign.charAt((year - 4) % 12);
	tmp += "年】 ";

	if (month < 1) {
		tmp += "闰 " + monthString.charAt(-month - 1);
	} else {
		tmp += monthString.charAt(month - 1);
	}

	tmp += "月";

	tmp += (day < 11) ? "初" : ((day < 20) ? "十" : ((day < 30) ? "廿" : "卅"));

	if (day % 10 != 0 || day == 10)
		tmp += numString.charAt((day - 1) % 10);

	return tmp;
}
